package com.yonyou.brigade.order_center.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.yonyou.brigade.order_center.model.BriTrainTicket;
import com.yonyou.brigade.order_center.dao.BriTrainTicketMapper;
import com.yonyou.brigade.order_center.model.BriTrainTicketDTO;
import com.yonyou.brigade.order_center.service.IBriTrainTicketService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yonyou.brigade.order_center.util.OrderConvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xuexmf@yonyou.com
 * @since 2018-09-06
 */
@Service
public class BriTrainTicketServiceImpl extends ServiceImpl<BriTrainTicketMapper, BriTrainTicket> implements IBriTrainTicketService {

    @Autowired
    private BriTrainTicketMapper briTrainTicketMapper;

    @Override
    public Map getBriTrainTicketDTOs(Map params) {
        Map queryParams=new HashMap();
        queryParams = params;
        queryParams.put("tenant",params.get("tenantId"));
        int pageSize=Integer.parseInt(params.get("pageSize").toString());
        int current=Integer.parseInt(params.get("current").toString());
        if(pageSize<=0) {
            pageSize=10;
        }
        if(current<=0) {
            current=1;
        }
        int begin=(current-1)*pageSize;
        queryParams.put("begin",begin);
        queryParams.put("pageSize",pageSize);
        Map result=new HashMap();
        try {
            List<BriTrainTicketDTO> briTrainTicketDTOs = briTrainTicketMapper.getBriTrainTicketDTOs(queryParams);
            int total=briTrainTicketMapper.getBriTrainTicketDTOsCount(queryParams);
            // 展示时将订单总金额平均分配
            if (!CollectionUtils.isEmpty(briTrainTicketDTOs)) {
                for (BriTrainTicketDTO briTrainTicketDTO : briTrainTicketDTOs) {
                    String orderNo = briTrainTicketDTO.getOrderNo();
                    Integer trainTicketCount = briTrainTicketMapper.selectCount(new EntityWrapper<BriTrainTicket>().where("order_no={0}", orderNo));
                    if (trainTicketCount > 1) {
                        briTrainTicketDTO.setPaymentPrice(String.valueOf(new BigDecimal(briTrainTicketDTO.getPaymentPrice()).divide(new BigDecimal(trainTicketCount),4,BigDecimal.ROUND_HALF_UP)));
                        briTrainTicketDTO.setPaperTicketFee(String.valueOf(new BigDecimal(briTrainTicketDTO.getPaperTicketFee()).divide(new BigDecimal(trainTicketCount),4,BigDecimal.ROUND_HALF_UP)));
                    }
                }
            }
            List<BriTrainTicketDTO> briTrainTicketDTOList = perfectTrainTicketDTOs(briTrainTicketDTOs);
            result.put("information",briTrainTicketDTOList);
            result.put("code","0");
            JSONObject pagination=new JSONObject();
            pagination.put("total",total);
            pagination.put("pageSize",pageSize);
            pagination.put("current",current);
            result.put("pagination",pagination);
        }
        catch (Exception e) {
            result.put("code","100");
            result.put("information","操作失败");
            e.printStackTrace();
        }
        return  result;
    }

    @Override
    public List<BriTrainTicket> getBriTrainTicketByOrderNoAndCardNo(String orderNo, String cardNo) {
        return briTrainTicketMapper.getBriTrainTicketByOrderNoAndCardNo(orderNo, cardNo);
    }

    @Override
    public List<BriTrainTicket> getBriTrainTicketByOrderNo(String orderNo) {
        return briTrainTicketMapper.getBriTrainTicketByOrderNo(orderNo);
    }

    private List<BriTrainTicketDTO> perfectTrainTicketDTOs(List<BriTrainTicketDTO> trainTicketDTOS) {
        List<BriTrainTicketDTO> trainTicketDTOList=new ArrayList<>();
        if(trainTicketDTOS==null) {
            return  trainTicketDTOS;
        }
        else {
            for(int i=0;i<trainTicketDTOS.size();i++) {
                trainTicketDTOList.add(OrderConvert.perfactTrainDTO(trainTicketDTOS.get(i)));
            }
        }
        return  trainTicketDTOList;
    }
}
